<?php
namespace app\front\service\v2;

use app\front\service\GetMsgService;
use app\front\model\Shop as ShopModel;
use app\front\model\Userinfo as UserinfoModel;
class Notify extends GetMsgService
{
    
    private static $_instance;
    
    //private 构造函数
    private function __construct() {
        // echo " I'm construct! process id is " . getmypid() . " and thread id is " . Thread::getCurrentThreadId() . "\n";
    }
    
    private function __clone() {
        //    echo " I'm clone! process id is " . getmypid() . " and thread id is " . Thread::getCurrentThreadId() . "\n";
    }
    
    //单例方法访问类实例
    public static function getInstance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    /**
     * 函数用途描述 : 定时器坏了给洪亮发通知
     * @date: 2018年2月5日 下午1:10:10
     * @author: 孙洪亮
     * @param: $out_trade_no（订单号）
     * @return:
     */
    public function alarmNotity(){
        $ACCESS_TOKEN=getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ACCESS_TOKEN;
        $users=[
            '0'=>'odRh-xFoqYqhl5vrcTwXA3Z9C2NI',
            '1'=>'odRh-xK_pRgfHAffz0cyWzSJ6Evk'
        ];
        
       for($i=0;$i<count($users);$i++){
           $params = [
               "touser"=>$users[$i],
               "template_id"=>'uKCaXkYgULbYOVz7Un4hr_4hIw59uVWRLxFAxHM1iKo',
               //"url"=>config('URL')."v2.ShowController/showTodayOrder?store_id=".$store_id,
               "data"=>[
                   "keyword1"=>[
                       "value"=>"定时器又双叒叕坏了！",
                       "color"=>"#173177"
                   ],
                   "keyword2"=> [
                       "value"=>date("Y-m-d H:i:s",time()),
                       "color"=>"#173177"
                   ]
               ]
           ];
           $params=json_encode($params);
           $res=https_request($url,$params);
        }

        
        
    }
    
 
    /**
     * 函数用途描述 : 根据订单号给老板发送模板消息
     * @date: 2018年2月5日 下午1:10:10
     * @author: 孙洪亮
     * @param: $out_trade_no（订单号）
     * @return:
     */
    public function NotityForBoss($out_trade_no){
        $TemporaryMsg=$this->getTemporaryForMchId($out_trade_no);
      
        $customer_id=$TemporaryMsg['open_id'];//获取客户号
        $store_id =$TemporaryMsg['store_id'];  //从表中获取商户ID
        $moneyNum = $TemporaryMsg['total_fee']; //从表中获取交易金额
        $moneyNum= bcdiv($moneyNum,100,3);
       
        $user=getMemberMessageForOpenid($customer_id);
       
        if($user['subscribe']==1){
            $nickname=$user['nickname'];
        }else{
            $nickname="非会员用户";
        }
      
        
        $storeMsg=$this->getStoreForStoreid($store_id);
        $shop_id= $storeMsg['shop_id']; 
        $store_name =$storeMsg['store_name']; 
        $imgurl = $storeMsg['imgurl']; 
        $phone = $storeMsg['phone']; 
        $address = $storeMsg['address']; 
        $weixin= ShopModel::where('id='. $shop_id)->field('weixin')->find();//老板的id
        $userid=$weixin['weixin'];
        $openid= UserinfoModel::where('id='. $userid)->field('openid')->find();
        $BoosOpenid=$openid['openid'];//老板的openid
        $res=$this->getTodayOrder($store_id);
        $TodayOrderNum=0;
        $todayMonetSum=0;
        foreach( $res as $v )
        {
            $TodayOrderNum= $TodayOrderNum+1;
            $todayMonetSum= bcadd($todayMonetSum,$v['money'],2);
        }
        logs("NotityForBossMSG.txt", $BoosOpenid);
        $ACCESS_TOKEN=getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ACCESS_TOKEN;
        $params = [
            "touser"=>$BoosOpenid,
            "template_id"=>config('template_id'),
            "url"=>config('URL')."v2.ShowController/showTodayOrder?store_id=".$store_id,
            "data"=>[
                "first"=> [
                    "value"=>"订单支付成功通知！",
                    "color"=>"#173177"
                ],
                "keyword1"=>[
                    "value"=>$out_trade_no,
                    "color"=>"#173177"
                ],
                "keyword2"=> [
                    "value"=>date("Y-m-d H:i:s",time()),
                    "color"=>"#173177"
                ],
                "keyword3"=> [
                    "value"=>sprintf('%.2f', (float)$moneyNum) ."元",
                    "color"=>"#173177"
                ] ,
                "keyword4"=> [
                    "value"=>"至富通扫码支付",
                    "color"=>"#173177"
                ],
                "remark"=>[
                    "value"=>"收款商家：".$store_name."\n"."支付客户：".$nickname."\n"."今日订单总数：".$TodayOrderNum."\n"."今日订单总金额：".$todayMonetSum,
                    "color"=>"#173177"
                ]
            ]
            
        ];
        $params=json_encode($params);
        
        $res=https_request($url,$params);
       logs("NotityForBoss.txt", $res);
        
        
    }
    
    /**
     * 函数用途描述 :通知顾客消费成功且点击详情进入到分享界面
     * @date: 2018年2月5日 下午1:07:08
     * @author: 孙洪亮
     * @param: $out_trade_no（订单号）
     * @return:
     */
    public function NotityForMember($out_trade_no,$store_id){
        $TemporaryMsg=$this->getTemporaryForMchId($out_trade_no);
        //获取客户
        // $customer_id=$this->getCustomer();
        //从表中获取商户ID
        $store_id =$TemporaryMsg['store_id'];
        //从表中获取交易金额
        $moneyNum = $TemporaryMsg['total_fee'];
        $moneyNum= bcdiv($moneyNum,100,3);
        $openid=$TemporaryMsg['open_id'];
        $store=$this->getStoreForStoreid($store_id);
        $store_name=$store['store_name'];
        $ACCESS_TOKEN=getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ACCESS_TOKEN;
        $params = [
            "touser"=>$openid,
            "template_id"=>config('template_id'),
            "url"=>config('shareWxURL')."?out_trade_no=".$out_trade_no,
            "data"=>[
                "first"=> [
                    "value"=>"收款商家：".$store_name,
                    "color"=>"#173177"
                ],
                "keyword1"=>[
                    "value"=>$out_trade_no,
                    "color"=>"#173177"
                ],
                "keyword2"=> [
                    "value"=>date("Y-m-d H:i:s",time()),
                    "color"=>"#173177"
                ],
                "keyword3"=> [
                    "value"=>sprintf('%.2f', (float)$moneyNum) ."元",
                    "color"=>"#173177"
                ] ,
                "keyword4"=> [
                    "value"=>"至富通扫码支付",
                    "color"=>"#173177"
                ],
                "remark"=>[
                    "value"=>"你好，点击详情进入并分享还可获得额外红包！",
                    "color"=>"#FF0000"
                ]
            ],
        ];
        $params=json_encode($params);
        $res=https_request($url,$params);
       logs("NotityMember.return.txt", $res);
    }
    
    /**
     * 函数用途描述 :通知顾客消费成功
     * @date: 2018年2月5日 下午1:07:08
     * @author: 孙洪亮
     * @param: $out_trade_no（订单号）
     * @return:
     */
    public function NotityForMemberNoShare($out_trade_no,$store_id){
        $TemporaryMsg=$this->getTemporaryForMchId($out_trade_no);
        $store_id =$TemporaryMsg['store_id'];
        //从表中获取交易金额
        $moneyNum = $TemporaryMsg['total_fee'];
        $moneyNum= bcdiv($moneyNum,100,3);
        $openid=$TemporaryMsg['open_id'];
        $store=$this->getStoreForStoreid($store_id);
        $store_name=$store['store_name'];
        $ACCESS_TOKEN=getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ACCESS_TOKEN;
        $params = [
            "touser"=>$openid,
            "template_id"=>config('template_id'),
            "url"=>"",
            "data"=>[
                "first"=> [
                    "value"=>'感谢您对至富通的支持！',
                    "color"=>"#173177"
                ],
                "keyword1"=>[
                    "value"=>$out_trade_no,
                    "color"=>"#173177"
                ],
                "keyword2"=> [
                    "value"=>date("Y-m-d H:i:s",time()),
                    "color"=>"#173177"
                ],
                "keyword3"=> [
                    "value"=>sprintf('%.2f', (float)$moneyNum) ."元",
                    "color"=>"#173177"
                ] ,
                "keyword4"=> [
                    "value"=>"至富通扫码支付",
                    "color"=>"#173177"
                ],
                "remark"=>[
                    "value"=>"收款商家：".$store_name,
                    "color"=>"#173177"
                ]
            ]
            
        ];
        $params=json_encode($params);
        $res=https_request($url,$params);
        logs("NotityMemberNOshare.txt", $res);
    }
}


